package com.heima.homeservicemanagementsystem.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.heima.homeservicemanagementsystem.config.annotation.RestControllerMapping;
import com.heima.homeservicemanagementsystem.module.Result;
import com.heima.homeservicemanagementsystem.service.VerifyService;
import com.heima.homeservicemanagementsystem.utils.CodeUtils;
import com.heima.homeservicemanagementsystem.utils.RedisClient;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;


/**
 * @Description
 * @Author jcf
 * @Date 2025/3/11
 */
@Tag(name = "验证控制器", description = "就是验证手机号或者是密码的")
@RestControllerMapping("/account")
public class verifyController {

    @Autowired
    private VerifyService verifyService;
    @Autowired
    private RedisClient redisClient;


    private static final Logger log = LoggerFactory.getLogger(verifyController.class);

    @SaIgnore
    @Operation(summary = "手机号验证码")
    @PostMapping("/sms")
    public Result sms(@RequestBody Map<String, String> requestData) {
        String phone = requestData.get("mobile");  // 获取手机号
//        log.info("接收到的手机: {}", phone);
//            return Result.success();
        return Result.success("发送成功", verifyService.getCode(phone));
    }

    @SaIgnore
    @PostMapping("/email")
    @Operation(summary = "邮箱验证码")
    public Result email(@RequestParam Map<String, String> requestData) {
        String email = requestData.get("email");

//        CompletableFuture.runAsync(() -> {
//            try {
//                verifyService.getemail(email);  // 发送验证码的业务逻辑
//            } catch (Exception e) {
//                // 处理发送失败的情况，可以记录日志或者做其他的补救措施
//                System.out.println("邮件发送失败：" + e.getMessage());
//            }
//        });
        String code = CodeUtils.generateCode();
        redisClient.set("emailCode-" + email, code, 1000L * 60);
        return Result.success(code);
//        return Result.success("发送成功,请注意查收哦？时间有点长哦~");


    }
}
